(()=> {
	
	function tabbar() {
		let oldTb = new Tabbar({
			tabId: 'tab-old',
			titleClass: 'old-title',
			contentClass: 'old-item',
			activeClass: 'active',
			callBack: (res) => {
				for(let i = 0; i < res.titleList.length; i++) {
					ele.listBox.innerHTML = ''
					// ele.myhave.style.display = 'none';
					if(~res.titleList[i].className.indexOf('active')) {
						// console.log(res.titleList[i].className.split(' ')[0]);
						wp_type = res.titleList[i].className.split(' ')[0];
						c_id = '';
						allData1 = [];
						// allData2 = [];
						// h_id = ele.select2.options[ele.select2.selectedIndex].value;
						houseShopList(page,type,wp_type,h_id,c_id);
						getOptions();
					}
				}
			}
		});
	}
	
	function tabbar2() {
		let newTb = new Tabbar({
			tabId: 'tab-new',
			titleClass: 'new-title',
			contentClass: 'new-item',
			activeClass: 'active',
			callBack: (res) => {
				for(let i = 0; i < res.titleList.length; i++) {
					ele.listBox.innerHTML = ''
					// ele.myhave.style.display = 'nlock';
					if(~res.titleList[i].className.indexOf('active')) {
						console.log(res.titleList[i].className.split(' ')[0]);
						wp_type2 = res.titleList[i].className.split(' ')[0];
						c_id2 = '';
						allData1 = [];
						// allData2 = [];
						// h_id = ele.select2.options[ele.select2.selectedIndex].value;
						houseShopList(page,type,wp_type2,h_id2,c_id2);
						getOptions();
					}
				}
			}
		});
	}
	

	let ele = {};

	ele.record = $('.record');
	ele.tabTitle = $('#tabTitle');
	ele.tabTitle2 = $('#tabTitle2');
	ele.select1 = $('#select1');
	ele.select2 = $('#select2');
	ele.select3 = $('#select3');
	ele.select4 = $('#select4');
	ele.select5 = $('#select5');
	ele.select6 = $('#select6');
	ele.select7 = $('#select7');
	ele.select8 = $('#select8');
	ele.select9 = $('#select9');
	ele.select10 = $('#select10');
	ele.select11 = $('#select11');
	ele.select12 = $('#select12');
	ele.modal_commission = $('#modal-commission');
	ele.close_commission = $('#close-commission');
	ele.optionBox = $('#optionBox');
	ele.listBox = $('#listBox');
	ele.more = $('#more');
	ele.empty = $('#empty');
	ele.modal_buy = $('#modal-buy');
	ele.close_buy = $('#close-buy');
	ele.buyImg = $('#buyImg');
	ele.buyIpt = $('#buyIpt');
	ele.buyJia = $('#buyJia');
	ele.buyJian = $('#buyJian');
	ele.buyMax = $('#buyMax');
	ele.buyName = $('#buyName');
	ele.buyMoney = $('#buyMoney');
	ele.buyGold = $('#buyGold');
	ele.buyConfirm = $('#buyConfirm');
	ele.optionTotem = $('#optionTotem');
	ele.modal_totem = $('#modal-totem');
	ele.close_totem = $('#close-totem');
	ele.optionDd = $('#optionDd');
	ele.modal_dd = $('#modal-dd');
	ele.close_dd = $('#close-dd');
	ele.optionFf = $('#optionFf');
	ele.modal_ff = $('#modal-ff');
	ele.close_ff = $('#close-ff');
	ele.option2s = $('.option2');
	ele.is_gm = $('#is_gm');
	ele.myhave = $('#myhave');
	
	//关闭 碎片弹窗 人才
	ele.close_commission.onclick = () => {
		closeMdl(ele.modal_commission);
	}
	//关闭 图腾
	ele.close_totem.onclick = () => {
		closeMdl(ele.modal_totem);
	}
	//关闭 地段
	ele.close_dd.onclick = () => {
		closeMdl(ele.modal_dd);
	}
	//关闭 家具
	ele.close_ff.onclick = () => {
		closeMdl(ele.modal_ff);
	}
	//关闭 购买
	ele.close_buy.onclick = () => {
		closeMdl(ele.modal_buy);
	}
	
	let page = 1;
	
	let type = 2;
	
	let wp_type = 0;
	
	let h_id = '';
	
	let c_id = '';
	
	let wp_type2 = 0;
	
	let h_id2 = '';
	
	let c_id2 = '';
	
	let goldNum = '';
	
	let allData1 = [];
	
	let allData2 = [];
	
	tabBar();
	// 选项卡选项功能
	function tabBar() {
		for (let i = 0; i < ele.option2s.length; i++) {
			ele.option2s[i].addEventListener('click', e => {
				let cls = e.target.className;
				type = cls.split(' ')[0];
				// console.log(type);
				if(type == 2) {
					allData1 = [];
					ele.myhave.style.display = 'none';
					houseShopList(page,type,wp_type,h_id,c_id);
				} else if (type == 1) {
					allData1 = [];
					ele.myhave.style.display = 'flex';
					houseShopList(page,type,wp_type2,h_id2,c_id2);
				}
			})
		}
	}
	
	marketList();
	// 市场分类列表
	function marketList() {
		let token = getCookie("token");
		ajax({
			url: API_PATH.PROP_LIST,
			method: 'GET',
			header: {
				'Authorization': token,
			},
			success: res => {
				if (res.code == 1) {
					// openTips(res.msg);
					drawList(res.data)
					// drawTotem(res.data.totem_list)
					tabbar();
					tabbar2();
				} else openTips(res.msg || '未知错误');
			}
		});
	}
	
	houseShopList(page,type,wp_type,h_id,c_id);
	// 市场列表  明细
	function houseShopList(page,type,wp_type,h_id,c_id) {
		ele.more.innerText = '加载中...';
		let token = getCookie("token");
		ajax({
			url: API_PATH.HOUSE_SHOP_LIST,
			method: 'GET',
			header: {
				'Authorization': token,
			},
			data: {
				page: page,
				type: type,
				wp_type: wp_type, //tarbar 
				h_id: h_id, // 房产
				c_id: c_id, //人才
			},
			success: res => {
				if (res.code == 1) {
					goldNum = res.data.gold
					let detail = res.data.list;
					// console.log(detail.data.length);
					allData1 = [...allData1,...detail.data];
					console.log(allData1);
					drawOption(detail.data,page);
					// console.log(detail.data.length);
					if (detail.data.length == 0) {
						page = 1;
						ele.more.style.display = "none";
						ele.empty.style.display = "block";
					} else {
						ele.more.style.display = "block";
						ele.empty.style.display = "none";
						// page++;
						if (page >= res.data.list.last_page) {
							ele.more.innerText = '没有更多了';
							ele.more.onclick = null;
						}
						else {
							ele.more.innerText = '加载更多';
							page++;
							if(type == 2) {
								ele.more.onclick = () => {
									// allData1 += detail.data;
									houseShopList(page,type,wp_type,h_id,c_id);
								}
							} else if(type == 1) {
								ele.more.onclick = () => {
									// allData2 += detail.data;
									houseShopList(page,type,wp_type2,h_id2,c_id2);
								}
							}
						}
					}
					
					let str = '';
					let rencai = res.data.rc_list
					// 人才重新渲染
					if(rencai != '' || rencai != null) {
						for(let i = 0; i < rencai.length; i++) {
							let pj_type = rencai[i].rc_info.type == 1 ? 'optiongreen' : rencai[i].rc_info.type == 2 ? 'optionzise' : 'optionorange';
							let rc_img = rencai[i].rc_info.lit_pic == null ? 'http://img.qxgs.cc/tiantianlaishouzu/img/rencai/renwu.png' : IMG_DOMAIN + rencai[i].rc_info.lit_pic;
							str += '<div class="option '+pj_type+'" data="'+rencai[i].id+'" name="'+rencai[i].name+'">'
											+'<div class="optionin" data="'+rencai[i].id+'" name="'+rencai[i].name+'">'
												+'<img src="'+rc_img+'" alt="">'
											+'</div>'
											+'<span class="name">'+rencai[i].name+'</span>'
										+'</div>'
						}
					}
					ele.optionBox.innerHTML = str;
					
					let optionss = $('.option')
					// console.log(options.length);
					for(let j = 0; j < optionss.length; j++) {
						optionss[j].onclick = () => {
							page = 1;
							closeMdl(ele.modal_commission);
							closeMdl(ele.modal_totem);
							closeMdl(ele.modal_dd);
							closeMdl(ele.modal_ff);
							closeMdl(ele.modal_buy);
							if(type == 2) {
								c_id = optionss[j].getAttribute('data');
								let nowName = optionss[j].getAttribute('name');
								ele.select1.innerHTML = nowName
								allData1 = [];
								houseShopList(page,type,wp_type,h_id,c_id);
							} else if (type == 1) {
								c_id2 = optionss[j].getAttribute('data');
								let nowName = optionss[j].getAttribute('name');
								ele.select7.innerHTML = nowName
								allData1 = [];
								houseShopList(page,type,wp_type2,h_id2,c_id2);
							}
						}
					}
					// openTips(res.msg);
				} else openTips(res.msg || '未知错误');
			}
		});
	}
	
	function drawOption(_data,pageNow) {
		// console.log(pageNow);
		let html = '';
		let is_sell = '';
		let pj_type = '';
		for(let i = 0; i < _data.length; i++) {
			let imgsrc = _data[i].class.logo == null ||  _data[i].class.logo == '' ? 'http://img.qxgs.cc/tiantianlaishouzu/img/luck/thingbg.png' : IMG_DOMAIN + _data[i].class.logo
			is_sell = type == 2 ? '购买' : '出售';
			let name = ~_data[i].class.name.indexOf('】') ? _data[i].class.name.split('】')[1]+'<br />'+_data[i].class.name.split('】')[0] + '】' : _data[i].class.name
			console.log( _data[i].class.hammer);
			pj_type = _data[i].class.hammer == 6 ? 'itemred' :  _data[i].class.hammer == 2 ? 'itemgreen' : _data[i].class.hammer == 3 ? 'itemblue' :  _data[i].class.hammer == 4 ? 'itempurple' :  _data[i].class.hammer == 5 ? 'itemorange' : '';
			html += '<div class="item">'
						+'<div class="left">'
						+'<div class="leftimg '+pj_type+'">'
							+'<img src="'+imgsrc+'">'
						+'</div>'
							+'<div class="leftin">'
								+'<div class="left1">'
									+'<p class="mobile">'+name+'</p>'
									+'<p class="number">数量:'+_data[i].lose_num+'</p>'
								+'</div>'
								+'<div class="left2">'
									+'<img src="http://img.qxgs.cc/tiantianlaishouzu/img/home/jinbi.png" alt="">'
									+'<p class="time">'+_data[i].money+'/个</p>'
								+'</div>'
							+'</div>'
						+'</div>'
						+'<div class="real">'
						+'<button class="gou" data="'+ _data[i].cid+'">'+is_sell+'</button>'
							// +'<button class="gou" onclick="goumai('+_data[i].id+','+_data[i].num+','+_data[i].money+','+_data[i].class.name+')">购买</button>'
						+'</div>'
					+'</div>'
		}
		if(pageNow > 1) {
			ele.listBox.innerHTML += html;
		} else {
			ele.listBox.innerHTML = html;
		}
		
		let gous = $('.gou');
		for(let j = 0; j < gous.length; j++) {
			gous[j].onclick = () => {
				if(type == 1) {
					console.log(gous[j].getAttribute('data'));
					houseOrderLoseNum(gous[j].getAttribute('data'))
				}
				console.log(gous[j]);
				ele.is_gm.innerHTML = is_sell
				ele.buyConfirm.innerHTML = is_sell
				console.log(j,IMG_DOMAIN + allData1[j].class.logo);
				ele.buyImg.src =  allData1[j].class.logo == null || allData1[j].class.logo == '' ? 'http://img.qxgs.cc/tiantianlaishouzu/img/luck/thingbg.png' : IMG_DOMAIN + allData1[j].class.logo
				if(type == 2) {
					ele.buyName.innerHTML = '购买【'+allData1[j].class.name+'】需要'
				} else if(type == 1) {
					ele.buyName.innerHTML = '出售【'+allData1[j].class.name+'】获得'
				}
				ele.buyMoney.innerHTML = Math.floor(allData1[j].money * 100) / 100 
				ele.buyIpt.value = '1'
				ele.buyGold.innerHTML = goldNum;
				openMdl(ele.modal_buy);
				ele.buyJia.onclick = ()=> {
					if(ele.buyIpt.value >= allData1[j].lose_num) {
						openTips('不能再加啦！')
					}else {
						ele.buyIpt.value++
						ele.buyMoney.innerHTML = Math.floor(allData1[j].money * ele.buyIpt.value * 100) / 100 
					}
				}
				ele.buyJian.onclick = ()=> {
					if(ele.buyIpt.value <= 1) {
						openTips('不能再减啦！')
					}else {
						ele.buyIpt.value--
						// ele.buyMoney.innerHTML = allData1[j].money * ele.buyIpt.value
						ele.buyMoney.innerHTML = Math.floor(allData1[j].money * ele.buyIpt.value * 100) / 100 
					}
				}
				ele.buyMax.onclick =() => {
					ele.buyIpt.value = allData1[j].lose_num
					// ele.buyMoney.innerHTML = ele.buyIpt.value * allData1[j].money
					ele.buyMoney.innerHTML = Math.floor(ele.buyIpt.value * allData1[j].money * 100) / 100 
				}
				ele.buyIpt.oninput = ()=> {
					
					// console.log(ele.buyIpt.value);
					if(ele.buyIpt.value > allData1[j].lose_num) {
						openTips('不能大于剩余数量')
						ele.buyIpt.value = allData1[j].lose_num
					} else if(ele.buyIpt.value < 0) {
						openTips('最小数量为1')
						ele.buyIpt.value = 1
					} else if(!/^\\d+$/.test(ele.buyIpt.value)) {
						// openTips('请输入正整数')
						ele.buyIpt.value = parseInt(ele.buyIpt.value)
						// return
					}
					// ele.buyMoney.innerHTML = allData1[j].money * ele.buyIpt.value
					ele.buyMoney.innerHTML = Math.floor(allData1[j].money * ele.buyIpt.value * 100) / 100
				}
				ele.buyConfirm.onclick = () => {
					if(ele.buyIpt.value == 0) {
						openTips('最小数量为1')
						ele.buyIpt.value = 1
						// ele.buyMoney.innerHTML = allData1[j].money * ele.buyIpt.value
						ele.buyMoney.innerHTML = Math.floor(allData1[j].money * ele.buyIpt.value * 100) / 100
					} else goumai(allData1[j].id,ele.buyIpt.value)
				}
			}
		}
	}
	
	
	function houseOrderLoseNum(id) {
		// console.log(id,num,money,name);
		let token = getCookie("token");
		ajax({
			url: API_PATH.HOUSE_ORDER_LOSE_NUM,
			method: 'GET',
			header: {
				'Authorization': token,
			},
			data: {
				id: id
			},
			success: res => {
				if (res.code == 1) {
					ele.myhave.innerHTML = '<span>当前道具拥有数量：</span><p>'+res.data+'</p>'
				} else openTips(res.msg || '未知错误');
			}
		});
	}
	
	
	function goumai(id,num) {
		// console.log(id,num,money,name);
		let token = getCookie("token");
		ajax({
			url: API_PATH.HOUSE_ORDER_ADD,
			method: 'POST',
			header: {
				'Authorization': token,
			},
			data: {
				id: id,
				num: num
			},
			success: res => {
				if (res.code == 1) {
					openTips(res.msg);
					closeMdl(ele.modal_buy)
					allData1 = [];
					// setTimeout(()=> {
						if(type == 2) {
							allData1 = [];
							houseShopList(page,type,wp_type,h_id,c_id);
						} else if (type == 1) {
							allData1 = [];
							houseShopList(page,type,wp_type2,h_id2,c_id2);
						}
						// location.reload(true);
					// },1000)
				} else openTips(res.msg || '未知错误');
			}
		});
	}
	
	function drawList(_data) {
		let clsactive = '';
		let html_rc = '';
		let html_totem = '';
		let html_dd = '';
		let html_ff = '';
		// tabbar
		for(let i = 0; i < _data.menu_list.length; i++) {
			clsactive = i == 0 ? 'active' : '';
			ele.tabTitle.innerHTML += '<button class="'+i+' old-title '+clsactive+'">'+_data.menu_list[i].name+'</button>'
			ele.tabTitle2.innerHTML += '<button class="'+i+' new-title '+clsactive+'">'+_data.menu_list[i].name+'</button>'
		}
		
		//房产
		for(let j = 0; j < _data.house_list.length; j++) {
			ele.select2.innerHTML += '<option value="'+_data.house_list[j].id+'">'+_data.house_list[j].jz_name+'</option>'
			ele.select8.innerHTML += '<option value="'+_data.house_list[j].id+'">'+_data.house_list[j].jz_name+'</option>'
			h_id = ele.select2.options[ele.select2.selectedIndex].value;
			h_id2 = ele.select8.options[ele.select8.selectedIndex].value;
			ele.select2.onchange = ()=> {
				// h_id = ele.select2.selectedIndex;
				// if(ele.select2.options[ele.select2.selectedIndex].value != '' ||ele.select2.options[ele.select2.selectedIndex].value != null) {
					h_id = ele.select2.options[ele.select2.selectedIndex].value;
					allData1 = [];
					houseShopList(page,type,wp_type,h_id,c_id);
				// } else 
				
				// console.log(ele.select2.options[ele.select2.selectedIndex].value);
			}
			ele.select8.onchange = ()=> {
				// h_id = ele.select2.selectedIndex;
				h_id2 = ele.select8.options[ele.select8.selectedIndex].value;
				allData1 = [];
				houseShopList(page,type,wp_type2,h_id2,c_id2);
				// console.log(ele.select2.options[ele.select2.selectedIndex].value);
			}
		}
		
		//人才
		for(let j = 0; j < _data.menu_list[0].list.length; j++) {
			page = 1;
			// let pj_type = _data[3].list[i].hammer == 6 ? 'itemred' :  _data[3].list[i].hammer == 2 ? 'optiongreen' : _data[3].list[i].hammer == 3 ? 'optionblue' :  _data[3].list[i].hammer == 4 ? 'optionzise' :  _data[3].list[i].hammer == 5 ? 'optionorange' : '';
			let pj_type = _data.menu_list[0].list[j].rc_info.type == 1 ? 'optiongreen' : _data.menu_list[0].list[j].rc_info.type == 2 ? 'optionzise' : 'optionorange';
			let rc_img = _data.menu_list[0].list[j].logo == null ? 'http://img.qxgs.cc/tiantianlaishouzu/img/rencai/renwu.png' : IMG_DOMAIN + _data.menu_list[0].list[j].logo
			html_rc += '<div class="option '+pj_type+'" data="'+ _data.menu_list[0].list[j].id+'" name="'+ _data.menu_list[0].list[j].name+'">'
						+'<div class="optionin">'
							+'<img src="'+rc_img+'" alt="">'
						+'</div>'
						+'<span class="name">'+_data.menu_list[0].list[j].name+'</span>'
					+'</div>'
			// ele.select1.innerHTML += '<option value="">'+_data.menu_list[0].list[j].name+'</option>'
		}
		ele.optionBox.innerHTML = html_rc;
		
		ele.select1.onclick = ()=> {
			openMdl(ele.modal_commission);
		}
		
		ele.select7.onclick = ()=> {
			openMdl(ele.modal_commission);
		}
		
		//图腾
		for(let j = 0; j < _data.menu_list[2].list.length; j++) {
			html_totem += '<div class="option optionblue" data="'+_data.menu_list[2].list[j].id+'">'
						// +'<div class="optionin">'
								+'<img src="'+IMG_DOMAIN+_data.menu_list[2].list[j].logo+'" alt="">'
						// +'</div>'
								+'<span class="name">'+_data.menu_list[2].list[j].name+'</span>'
						+'</div>'
			ele.optionTotem.innerHTML = html_totem;
			
			ele.select3.onclick = ()=> {
				openMdl(ele.modal_totem);
			}
			
			ele.select9.onclick = ()=> {
				openMdl(ele.modal_totem);
			}
			// ele.select3.innerHTML += '<option value="">'+_data.menu_list[2].list[j].name+'</option>'
		}
		
		//地段
		for(let j = 0; j < _data.menu_list[3].list.length; j++) {
			html_dd += '<div class="option optionblue"  data="'+_data.menu_list[3].list[j].id+'">'
						// +'<div class="optionin">'
								+'<img src="'+IMG_DOMAIN+_data.menu_list[3].list[j].logo+'" alt="">'
						// +'</div>'
								+'<span class="name">'+_data.menu_list[3].list[j].name+'</span>'
						+'</div>'
			ele.optionDd.innerHTML = html_dd;
			
			ele.select4.onclick = ()=> {
				openMdl(ele.modal_dd);
			}
			
			ele.select10.onclick = ()=> {
				openMdl(ele.modal_dd);
			}
			// ele.select4.innerHTML += '<option value="">'+_data.menu_list[3].list[j].name+'</option>'
		}
		//家具
		for(let j = 0; j < _data.menu_list[4].list.length; j++) {
			html_ff += '<div class="option optionblue"  data="'+_data.menu_list[4].list[j].id+'">'
							// +'<img src="'+IMG_DOMAIN+_data.menu_list[4].list[j].logo+'" alt="">'
							// +'<div class="optionjj">'
								+'<img src="'+IMG_DOMAIN+_data.menu_list[4].list[j].logo+'" alt="">'
								+'<span class="name">'+_data.menu_list[4].list[j].name+'</span>'
							// +'</div>'
						+'</div>'
			ele.optionFf.innerHTML = html_ff;
			
			ele.select5.onclick = ()=> {
				openMdl(ele.modal_ff);
			}
			
			ele.select11.onclick = ()=> {
				openMdl(ele.modal_ff);
			}
			// ele.select5.innerHTML += '<option value="">'+_data.menu_list[4].list[j].name+'</option>'
		}
		
		//矿场
		for(let j = 0; j < _data.menu_list[5].list.length; j++) {
			ele.select6.innerHTML += '<option value="'+_data.menu_list[5].list[j].id+'">'+_data.menu_list[5].list[j].name+'</option>'
			ele.select12.innerHTML += '<option value="'+_data.menu_list[5].list[j].id+'">'+_data.menu_list[5].list[j].name+'</option>'
		}
		ele.select6.onchange = ()=> {
			c_id = ele.select6.options[ele.select6.selectedIndex].value;
			allData1 = [];
			houseShopList(page,type,wp_type,h_id,c_id);
			console.log(ele.select6.options[ele.select6.selectedIndex].value);
		}
		
		ele.select12.onchange = ()=> {
			c_id = ele.select12.options[ele.select12.selectedIndex].value;
			allData1 = [];
			houseShopList(page,type,wp_type2,h_id2,c_id2);
			console.log(ele.select6.options[ele.select6.selectedIndex].value);
		}
		
		// setTimeout(()=> {
			getOptions();
		// },2000)
	}
	
	
	
	function getOptions() {
		let options = $('.option')
		for(let i = 0; i < options.length; i++) {
			options[i].onclick = ()=> {
				// console.log(options[i]);
				closeMdl(ele.modal_commission);
				closeMdl(ele.modal_totem);
				closeMdl(ele.modal_dd);
				closeMdl(ele.modal_ff);
				closeMdl(ele.modal_buy);
				allData1 = [];
				allData2 = [];
				setTimeout(()=> {
					page = 1;
					if(type == 2) {
						c_id = options[i].getAttribute('data');
						let nowName = options[i].getAttribute('name');
						ele.select1.innerHTML = nowName
						allData1 = [];
						houseShopList(page,type,wp_type,h_id,c_id);
					} else if(type == 1) {
						c_id2 = options[i].getAttribute('data');
						let nowName = options[i].getAttribute('name');
						ele.select7.innerHTML = nowName
						allData1 = [];
						houseShopList(page,type,wp_type2,h_id2,c_id2);
					}
				},500)
			}
		}
		
	}
	
	
})()